﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xinkui.Spider.DataAccess;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Visitors;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Util;

namespace Xinkui.Spider.Spider
{
    public class CompanySpider
    {
        /// <summary>
        /// 待抓取的公司
        /// </summary>
        private CompanyEntity SpiderCompany
        { get; set; }

        public CompanySpider(string link, string name)
        {
            SpiderCompany = DBHelper.GetCompanyByLink(link);
            if (SpiderCompany == null)
            {
                SpiderCompany = new CompanyEntity();
                SpiderCompany.SpiderLink = link;
                SpiderCompany.CompanyName = name;
                SpiderCompany.IsNew = 1;
            }

            Spidering();
        }

        private void Spidering()
        {
            //开线程抓取
            string listPage = HttpUtil.PostPage(SpiderCompany.SpiderLink, null);
            Lexer lex = new Lexer(listPage);
            Parser parser = new Parser(lex);
            //HtmlPage page = parser.Parse(null);

            //NodeList htmlNodes = parser.Parse(null);
            //INode findNode = null;

            NodeList nodelist = parser.Parse(null);
            NodeFilter filter = new Winista.Text.HtmlParser.Filters.AttributeRegexFilter("class", "company-description company-description-full");
            nodelist = nodelist.ExtractAllNodesThatMatch(filter, true);
            for (int i = 0; i < nodelist.Size(); i++)
            {
                
                System.Console.Write(nodelist.ElementAt(i).ToPlainTextString() + "\n");
            }



            //for (int i = 0; i < htmlNodes.Count && findNode == null; i++)
            //{
            //    findNode = FindNodo(htmlNodes[i],false, "company-description company-description-full",string.Empty);

            //}
        }



        private INode FindNodo(INode htmlNode, bool isSibling
            , string queryClass
            , string queryID)
        {
            INode returnNode = null;
            if (htmlNode == null)
                return returnNode;

            if (htmlNode is ITag)
            {
                ITag tag = (htmlNode as ITag);
                if (!tag.IsEndTag())
                {
                    if (tag.Attributes != null && tag.Attributes.Count > 0)
                    {
                        if (tag.Attributes["CLASS"] != null && queryClass.ToLower() == tag.Attributes["CLASS"].ToString().ToLower())
                        {
                            return htmlNode;
                        }
                    }
                }
            }

            //the children nodes
            if (htmlNode.Children != null && htmlNode.Children.Count > 0)
            {
                returnNode = this.FindNodo(htmlNode.FirstChild, true, queryClass, queryID);
                if (returnNode != null)
                {
                    return returnNode;
                }
            }

            //the sibling nodes
            if (isSibling)
            {
                INode sibling = htmlNode.NextSibling;
                while (sibling != null)
                {
                    returnNode = this.FindNodo(sibling, false, queryClass, queryID);
                    if (returnNode == null)
                    {
                        sibling = sibling.NextSibling;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return returnNode;
        }
    }
}
